//
//  HTEpisodesPopupView.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/25.
//

import SwiftUI
import SDWebImageSwiftUI

struct HTEpisodesPopupView: View {

  @Binding var var_showEpisodesPopupView: Bool
  @Binding var var_showInterstitialAd: Bool
  @State var var_recentPlayFirst: HTAnime = HTAnime()
  @State private var var_popOffset = UIDevice.ht_screenHeight() + 70
  @State private var var_id: String = ""
  @State private var var_epId: String = ""
  @State private var var_pushToVideoDetail = false
  @State private var var_delayTask: DispatchWorkItem = DispatchWorkItem {}

  init(var_showEpisodesPopupView: Binding<Bool>, var_showInterstitialAd: Binding<Bool>, var_recentPlayFirst: HTAnime) {
    _var_showEpisodesPopupView = var_showEpisodesPopupView
    _var_showInterstitialAd = var_showInterstitialAd
    _var_recentPlayFirst = State(initialValue: var_recentPlayFirst)
  }

  var body: some View {
    GeometryReader { geometry in
      ZStack {
        NavigationLink(destination: HTVideoDetailScreen(var_id: var_id, var_epId: var_epId, var_seekTime: var_recentPlayFirst.var_startTime, var_source: "5"), isActive: $var_pushToVideoDetail) {
          EmptyView()
        }
        
        HStack(spacing: 0){
          Button {
            var_id = var_recentPlayFirst.var_id
            var_epId = var_recentPlayFirst.var_cur_epId ?? ""
            var_pushToVideoDetail = true
            var_popOffset = geometry.size.height + 70
            HTNetworkManager().ht_requestForBuried(.htEnumMomeMClNew(var_kid: "9", var_c_id: var_recentPlayFirst.var_id, var_c_name: var_recentPlayFirst.var_title ?? ""))
            if (!var_showInterstitialAd) {
              DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "hide"])
              }
            }
          } label: {
            WebImage(url: URL(string: var_recentPlayFirst.var_cover ?? ""))
              .resizable()
              .frame(width: 38, height: 60)
              .cornerRadius(10)
          }
          VStack(alignment: .leading, spacing: 8){
            Text(var_recentPlayFirst.var_title ?? "")
              .font(.system(size: 14))
              .foregroundColor(Color.white)
              .lineLimit(1)
            Text("From \(var_recentPlayFirst.var_startTime ?? "")")
              .font(.system(size: 14))
              .fontWeight(.medium)
              .foregroundColor(Color.white)
          }
          .padding(.horizontal, 15)
          Spacer()
          Button {
            var_id = var_recentPlayFirst.var_id
            var_epId = var_recentPlayFirst.var_cur_epId ?? ""
            var_pushToVideoDetail = true
            var_popOffset = geometry.size.height + 70
            HTNetworkManager().ht_requestForBuried(.htEnumMomeMClNew(var_kid: "9", var_c_id: var_recentPlayFirst.var_id, var_c_name: var_recentPlayFirst.var_title ?? ""))
            if (!var_showInterstitialAd) {
              DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "hide"])
              }
            }
          } label: {
            HStack{
              Text("Watch Now")
                .font(.system(size: 14))
                .fontWeight(.medium)
                .ht_normal()
            }
            .frame(width: 100, height: 35)
            .background(Color.white)
            .cornerRadius(6)
          }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .padding(.leading, 5)
        .padding(.trailing, 15)
        .background(Color(hex: "#4F40BD"))
        .cornerRadius(10)
      }
      .frame(maxWidth: .infinity, maxHeight: 70)
      .padding(.horizontal, 20)
      .offset(y: var_popOffset)
      .animation(.easeInOut)
      .onChange(of: var_showInterstitialAd, perform: { newValue in
        if (newValue) {
          var_delayTask.cancel()
        } else {
          var_delayTask = DispatchWorkItem {
            if (!var_showInterstitialAd) {
              var_popOffset = geometry.size.height + 70
              DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "hide"])
              }
            }
          }
          DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: var_delayTask)
        }
      })
      .onChange(of: var_showEpisodesPopupView, perform: { newValue in
        if (newValue) {
          var_popOffset = geometry.size.height - 80 - UIDevice.ht_tabBarFullHeight()

          var_delayTask = DispatchWorkItem {
            if (!var_showInterstitialAd) {
              var_popOffset = geometry.size.height + 70
              DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                NotificationCenter.default.post(name: HTConfig.STATIC_notification_showEpisodesPopupView, object: nil, userInfo: ["status": "hide"])
              }
            }
          }
          DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: var_delayTask)
        }
      })
    }
  }
}
